<?php
$objXajax->registerFunction("fmfrmreportrevenue_load");
$objXajax->registerFunction("fmfrmreportrevenue_deleteRevenue"); 

function fmfrmreportrevenue_load($dateFrom,$dateTo,$reporttype=1){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrSum = array();
    $arrData = fmfrmreportrevenue_createArray($dateFrom,$dateTo,$arrSum,$reporttype);
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_reportrevenue_grid'.$reporttype.'.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $arrSum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('fmfrmreportrevenue_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('fmfrmreportrevenue_tbl',true);");  
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportrevenue_createArray($dateFrom,$dateTo,&$arrSum,$reporttype=1){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    $sqlGroup='';
    $sql = '';
    switch($reporttype){
        case 1:  $sql = "SELECT * FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = $hotel_id order by date_sum ";        
        break;
        case 2: $sql = "SELECT DATE_FORMAT(date_sum, '%u') as woy,date_add(date_sum,interval -(DATE_FORMAT(date_sum, '%w ')+6)%7 day) as startweek,
        date_add(date_sum,interval (7-DATE_FORMAT(date_sum, '%w '))%7 day) as endweek,sum(money_total) as money_total,
        sum(other_pay) as other_pay,sum(other_get) as other_get ,sum(money_revenue) as money_revenue 
        FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = $hotel_id group by DATE_FORMAT(date_sum, '%u') ";  
        break;      
        case 3: $sql = "  SELECT DATE_FORMAT(date_sum, '%m') as moy,sum(money_total) as money_total,
        sum(other_pay) as other_pay,sum(other_get) as other_get,sum(money_revenue) as money_revenue
        FROM fm_summary WHERE datediff('$dateFrom',date_sum)<=0 and datediff('$dateTo',date_sum)>=0 and service_id = $hotel_id group by DATE_FORMAT(date_sum, '%m')"; 
    break;
    }
    $arr = array();
    try{
        $money_total =0;
        $other_get =0;
        $other_pay =0;
        $money_revenue =0;
        $arr = $objDbUpdate->GetArray($sql);
        $count=count($arr);
        for($i=0;$i<$count;$i++){
           $money_total += $arr[$i]['money_total']; 
           $other_get += $arr[$i]['other_get']; 
           $other_pay += $arr[$i]['other_pay']; 
           $money_revenue += $arr[$i]['money_revenue']; 
        }
        $arrSum[0]['money_total']= $money_total;
        $arrSum[0]['other_get']= $other_get;
        $arrSum[0]['other_pay']= $other_pay;
        $arrSum[0]['money_revenue']= $money_revenue;
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
} 
function fmfrmreportrevenue_loadRoom(){
    global $objDbUpdate,$strOption,$hotel_id;
    $strOption = createCombobox("fm_room",array('name','id'),array('name'=>'name','value'=>'id'),'fmfrmreportrevenue_cbbroom',true,'WHERE service_id='.$hotel_id); 
    $objResponse = new xajaxResponse();    
    $objResponse->addAssign('fmfrmreportrevenue_tdcbb', 'innerHTML', $strOption);
    $objResponse->addScript('fmfrmreportrevenue_createReport();');
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function fmfrmreportrevenue_deleteRevenue($dateDelete){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax      
    if($_SESSION['current']['power_type_id']==POWER_CUSTOMER){
        $dateDelete = addslashes($dateDelete); 
        $sql = "delete from fm_summary where service_id = $hotel_id and datediff(date_sum,'$dateDelete')<0 and datediff(date_sum,now())<0 ";
        if($objDbUpdate->Execute($sql)){
          $objResponse->addScript('fmfrmreportrevenue_createReport();');   
        }else{
           $objResponse->addAlert('không xóa được dữ liệu'); 
        }    
        
    }else{
        $objResponse->addAlert('Bạn không đủ quyền để xóa');  
    }

    $objResponse->addScript('changeWaiting(1);'); 
     return $objResponse->getXML(); // dong luon luon co khi goi ham ajax                       
} 

# end region process data
?>
